﻿Imports BUS
Imports DTO
Public Class DMLoaiSach
    Enum TrangThai
        xem
        Thêm
        sua
    End Enum
    Dim Loai As LoaiSachDTO
    Dim MaCu As String = ""
    Dim trth As TrangThai = TrangThai.xem
    Sub loadata()
        LoaiSach_DTOBindingSource.DataSource = LoaiSachBUS.DanhSach_Loai_BUS
    End Sub
    Sub chinhtt(ByVal tt As TrangThai)
        Select Case tt
            Case TrangThai.xem
                cbmaloaisach.Enabled = False
                tbtenloaisach.Enabled = False
                btthem.Enabled = True
                btsua.Enabled = True
                btxoa.Enabled = True
                btluu.Enabled = False
                bthuy.Enabled = False
                LoaiSach_DTODataGridView.ReadOnly = True
                Exit Select
            Case TrangThai.Thêm
                cbmaloaisach.Enabled = True
                tbtenloaisach.Enabled = True
                btthem.Enabled = False
                btsua.Enabled = False
                btxoa.Enabled = False
                btluu.Enabled = True
                bthuy.Enabled = True
                LoaiSach_DTODataGridView.ReadOnly = True
                Exit Select
            Case TrangThai.sua
                cbmaloaisach.Enabled = True
                tbtenloaisach.Enabled = True
                btthem.Enabled = False
                btsua.Enabled = False
                btxoa.Enabled = False
                btluu.Enabled = True
                bthuy.Enabled = True
                LoaiSach_DTODataGridView.ReadOnly = True
                Exit Select
        End Select
    End Sub
    Private Sub DMLoaiSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        chinhtt(trth)
        loadata()
    End Sub

    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        trth = TrangThai.Thêm
        chinhtt(trth)
        Loai = LoaiSach_DTOBindingSource.AddNew
    End Sub

    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        trth = TrangThai.sua
        chinhtt(trth)
        MaCu = cbmaloaisach.Text
        Loai = LoaiSach_DTOBindingSource.Current
    End Sub

    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        Loai = LoaiSach_DTOBindingSource.Current
        Dim kq = ""
        If (MessageBox.Show("Bạn có muốn xóa?", "Thông báo", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK) Then
            kq = LoaiSachBUS.Xoa_Loai_BUS(Loai)
            If (kq = "true") Then
                MessageBox.Show("Xóa thành công")
                trth = TrangThai.xem
                chinhtt(trth)
                loadata()
            Else
                MessageBox.Show("Xóa không thành công " + kq)
            End If
        End If
    End Sub

    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        Dim kq = ""
        If trth = TrangThai.Thêm Then
            kq = LoaiSachBUS.Them_Loai_BUS(Loai)
            If (kq = "true") Then
                MessageBox.Show("Thêm thành công")
                trth = TrangThai.xem
                chinhtt(trth)
                loadata()
            Else
                MessageBox.Show("Thêm không thành công " + kq)
            End If
        ElseIf trth = TrangThai.sua Then
            kq = LoaiSachBUS.Sua_Loai_BUS(Loai, MaCu)
            If (kq = "true") Then
                MessageBox.Show("Sửa thành công")
                trth = TrangThai.xem
                chinhtt(trth)
                loadata()
            Else
                MessageBox.Show("Sửa không thành công " + kq)
            End If
        End If
    End Sub

    Private Sub bthuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bthuy.Click
        trth = TrangThai.xem
        chinhtt(trth)
        loadata()
    End Sub

End Class